package cn.fl.capital.service.impl;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.fl.capital.condition.CapPrjRuleRQueryCondition;
import cn.fl.capital.transform.CapPrjRuleRVOTransform;
import cn.fl.capital.vo.CapPrjRuleRVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.capital.dto.CapAdvRuleInfoDTO;
import cn.fl.capital.dto.CapPrjRuleRDTO;
import cn.fl.capital.condition.CapAdvRuleInfoQueryCondition;
import cn.fl.capital.condition.CapPrjRuleRQueryCondition;
import cn.fl.capital.service.ICapAdvRuleInfoApiService;
import cn.fl.capital.service.ICapAdvRuleInfoService;
import cn.fl.capital.transform.CapAdvRuleInfoVOTransform;
import cn.fl.capital.vo.CapAdvRuleInfoVO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.List;
import java.util.Map;

/**
 * @Description: 垫款业务规则表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapAdvRuleInfoServiceImpl implements ICapAdvRuleInfoService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapAdvRuleInfoServiceImpl.class);
	
	/**
	 * 垫款业务规则表 暴露服务
	 */
	@Autowired
	private ICapAdvRuleInfoApiService capAdvRuleInfoApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public CapAdvRuleInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapAdvRuleInfoDTO dto = this.capAdvRuleInfoApiService.selectCapAdvRuleInfoById(id);
		return CapAdvRuleInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapAdvRuleInfoVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capAdvRuleInfoApiService.addCapAdvRuleInfo(CapAdvRuleInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capAdvRuleInfoApiService.removeCapAdvRuleInfoByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapAdvRuleInfoVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capAdvRuleInfoApiService.modifyCapAdvRuleInfo(CapAdvRuleInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapAdvRuleInfoQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapAdvRuleInfoQueryCondition());
        }
		condition=(CapAdvRuleInfoQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capAdvRuleInfoApiService.searchCapAdvRuleInfoListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapAdvRuleInfoVO> voList=CapAdvRuleInfoVOTransform.toVOList((List<CapAdvRuleInfoDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapAdvRuleInfoVO> searchList(CapAdvRuleInfoQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapAdvRuleInfoQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapAdvRuleInfoVO> voList=CapAdvRuleInfoVOTransform.toVOList(this.capAdvRuleInfoApiService.searchCapAdvRuleInfoList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapAdvRuleInfoVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapAdvRuleInfoConstant.CODE_CD);
		for(CapAdvRuleInfoVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public DataResultDTO addAndModifyCapPrjRuleR(CapPrjRuleRDTO capPrjRuleRDTO)
			throws BaseException {
		// TODO Auto-generated method stub
		
		return this.capAdvRuleInfoApiService.addAndModifyCapPrjRuleR(capPrjRuleRDTO);
	}

	@Override
	public List<CapAdvRuleInfoDTO> selectAdvRuleInfoByPrjId(
			CapPrjRuleRQueryCondition condition) throws BaseException {
		// TODO Auto-generated method stub
		List<CapAdvRuleInfoDTO>  list= this.capAdvRuleInfoApiService.selectAdvRuleInfoByPrjId(condition);
		if (list!=null && list.size()>0) {
			for (int i = 0; i < list.size(); i++) {
				
				CapAdvRuleInfoDTO rowdata=list.get(i);
				String h="";
				if(StringUtils.isNotEmpty(rowdata.getAdvCycCdNm())  && StringUtils.isNotEmpty(rowdata.getAdvTmCdNm())){
	                h +=rowdata.getAdvCycCdNm()+" "+rowdata.getAdvTmCdNm();
	            }else if(StringUtils.isNotEmpty(rowdata.getAdvCycCdNm())){
	                h +=rowdata.getAdvCycCdNm() ;
	            }else if(StringUtils.isNotEmpty(rowdata.getAdvTmCdNm())){
	                h +=rowdata.getAdvTmCdNm() ;
	            }
				rowdata.setAdvCycCdNm(h);
				h="";
	            if(StringUtils.isNotEmpty(rowdata.getAdvMinAmt()) && (rowdata.getAdvMaxAmt() == null || rowdata.getAdvMaxAmt().equals("") )){
	                h += ">="+rowdata.getAdvMinAmt();
	            }else if(StringUtils.isNotEmpty(rowdata.getAdvMinAmt())){
	                h += rowdata.getAdvMinAmt();
	            }
	            if(StringUtils.isNotEmpty(rowdata.getAdvMinAmt()) && StringUtils.isNotEmpty(rowdata.getAdvMaxAmt())){
	                h += "~";
	            }
	            if(StringUtils.isNotEmpty(rowdata.getAdvMaxAmt()) && (rowdata.getAdvMinAmt() == null || rowdata.getAdvMinAmt().equals("") )){
	                    h += "<="+rowdata.getAdvMaxAmt();
	            }else if(StringUtils.isNotEmpty(rowdata.getAdvMaxAmt())){
	                h += rowdata.getAdvMaxAmt();
	            }
	            rowdata.setAdvMinAmtNm(h);
	            h="";
	            if(StringUtils.isNotEmpty(rowdata.getMinOvdDay()) && (rowdata.getMaxOvdDay() == null || rowdata.getMaxOvdDay().equals("") )){
	                h += ">="+rowdata.getMinOvdDay();
	            }else if(StringUtils.isNotEmpty(rowdata.getMinOvdDay())){
	                h += rowdata.getMinOvdDay();
	            }
	            if(StringUtils.isNotEmpty(rowdata.getMinOvdDay()) && StringUtils.isNotEmpty(rowdata.getMaxOvdDay())){
	                h += "~";
	            }
	            if(StringUtils.isNotEmpty(rowdata.getMaxOvdDay()) && (rowdata.getMinOvdDay() == null || rowdata.getMinOvdDay().equals("") )){
	                    h += "<="+rowdata.getMaxOvdDay();
	            }else if(StringUtils.isNotEmpty(rowdata.getMaxOvdDay())){
	                h += rowdata.getMaxOvdDay();
	            }
	            rowdata.setMinOvdDayNm(h);
	        	
			}
		}
		return list;
	}


	

	/**
	 * 手动新增垫款规则
	 * @param entity
	 * @param dataList
	 * @return
	 */
	@Override
	public Long addRuleInfoByManual(CapAdvRuleInfoVO entity,List dataList,Long crtUsrId) throws BaseException {
		if(null == entity || CollectionUtils.isEmpty(dataList) || StringUtils.isEmpty(crtUsrId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.capAdvRuleInfoApiService.addRuleInfoByManual(CapAdvRuleInfoVOTransform.toDTO(entity),dataList,crtUsrId);
	}

	/**
	 * 通过关联表条件查询符合条件的中间表信息
	 * @param condition
	 * @return
	 */
	@Override
	public List<CapPrjRuleRVO> selectCapPrjRuleRList(CapPrjRuleRQueryCondition condition) throws BaseException{
		if(null == condition){
			return null ;
		}
		return CapPrjRuleRVOTransform.toVOList(this.capAdvRuleInfoApiService.selectCapPrjRuleRList(condition));
	}

}
